/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aiaudio.lastfm;

import aiaudio.processing.base.helpers.latex.TexFile;
import aiaudio.lastfm.hbase.*;
import aiaudio.Application;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;

/**
 *
 * @author nastya
 */
public class LastFmDownloaderHBase {

    public static final String API_KEY = "f1c35f7533cf155dcdefde21bb8f6c3d";
    private static final String SECRET_KEY = "11a35a18616065258fef354bdec06bff";

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        TexFile texFile = null;
        try {
            int num = 3;
            Configuration database = DatabaseConnector.connect();
            Configuration cc = new HBaseConfiguration(database);
//            SchemaCreationUtils m = new SchemaCreationUtils(database, TableCreationMode.ADD_TO_EXISTING);
//            String databaseName = "simple_" + num;
            String databaseName = "big_database";
//            DatabaseControl.init(m, databaseName);
            Application.init(databaseName);
            fillDatabase();
//            DatabaseControl.getInstance().clearFromTemporary();
////            
//            String key = "tst";
//            ProcessingTablePackage tablePackage = ProcessingTablePackageFactory.createProcessingTablePackage(key);
//            tablePackage.prepareDatabase();
//            ProcessingAlgorithm alg = new ProcessingAlgorithm(tablePackage);
//            alg.setSplitCoefficient(0.5);
//            alg.setInitialData(PreprocessRatingsAlgorithm.InitialData.LISTEN_COUNT);
//            alg.setNormalizaton(PreprocessRatingsAlgorithm.Normalizaton.SIMPLE);
//            alg.setDefaultValue(PreprocessRatingsAlgorithm.DefaultValue.ZERO);
//            alg.setHashingK(3);
//            alg.setHashingL(50);
//            alg.setHashingVectorTestSetSize(75);
//            alg.setNetworkGrouppingMinSimilarity(0.4);
//            alg.setNetworkLearningCicles(10);
//            alg.setNetworkTrainingSetSize(30);
//            alg.setNetworkUserDivisor(10);
//            alg.setMaxPositionCount(10);
//            alg.start();


        } catch (MasterNotRunningException ex) {
            Logger.getLogger(LastFmDownloaderHBase.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ZooKeeperConnectionException ex) {
            Logger.getLogger(LastFmDownloaderHBase.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(LastFmDownloaderHBase.class.getName()).log(Level.SEVERE, null, ex);
        } //        catch (CannotCreateTableException ex) {
        //            Logger.getLogger(LastFmDownloaderHBase.class.getName()).log(Level.SEVERE, null, ex);
        //        } //        catch (InterruptedException ex) {
        //            Logger.getLogger(LastFmDownloaderHBase.class.getName()).log(Level.SEVERE, null, ex);
        //        } catch (ClassNotFoundException ex) {
        //            Logger.getLogger(LastFmDownloaderHBase.class.getName()).log(Level.SEVERE, null, ex);
        //        } 
        catch (Exception ex) {
            Logger.getLogger(LastFmDownloaderHBase.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
        }
    }

    protected static void fillDatabase() throws CannotCreateTableException, IOException {
        SchemeCommunicator com1 = new SchemeCommunicator();
        SchemeCommunicator com2 = new SchemeCommunicator();
        UserNeighboursExtracter ex = new UserNeighboursExtracter("5537923", com1, com2);
        ex.start();

    }

    protected static void makeSnapshot(String databaseName, int i) throws IOException {
        String name = i > 0 ? databaseName + "_time_snapshot_" + i + ".tex" : databaseName + "_time_snapshot.tex";

        LatexSnapshotProducer latexSnapshotProducer = new LatexSnapshotProducer(name);
        latexSnapshotProducer.create();
        latexSnapshotProducer.close();
    }

    private static void closeFile(TexFile texFile) {
        if (texFile != null) {
            try {
                texFile.close();
            } catch (IOException ex) {
                Logger.getLogger(LastFmDownloaderHBase.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
